import turtle
import random
import time

def ball(pos):
    turtle.up()
    turtle.goto(pos)
    turtle.down()
    turtle.dot(20, 'gold')


def game():
    turtle.clear()
    end_time = time.time()
    turtle.up()
    turtle.goto(-300, 300)
    turtle.down()
    turtle.write(f"{int(end_time-start_time)}S",False, 'center', ('Arial', 30, 'normal'))
    global n
    if n == 0:
        for i in range(9):
            x = 340
            y = random.randint(-340, 340)
            points.append([x, y])
    n+=1
    if n == 3:
        n=0
    ball(pos_star)
    clear_points = []
    is_stop = False
    for i in points:
        turtle.up()
        turtle.goto(i)
        turtle.down()
        turtle.dot(20, 'cyan')
        if i[0] <-340:
            clear_points.append(i)
        if abs(i[0]-pos_star[0]) < 20 and abs(i[1]-pos_star[1])<20:
            is_stop = True
        i[0] -= 20
    for i in clear_points:
        points.remove(i)
    pos_star[1] -= 10
    if pos_star[1] > 370 or pos_star[1] < -350 or is_stop:
        pass
    else:
        turtle.ontimer(game, 200)



def up():
    pos_star[1]+=20

def left():
    pos_star[0]-=20

def right():
    pos_star[0]+=20

def down():
    pos_star[1]-=20

if __name__ == '__main__':
    points = []
    n = 0
    start_time = time.time()
    pos_star = [0, 340]
    turtle.tracer(False)
    turtle.onkeypress(up, "Up")
    turtle.onkeypress(left, "Left")
    turtle.onkeypress(right, "Right")
    turtle.onkeypress(down, "Down")
    game()
    turtle.listen()
    turtle.done()



